package com.wap.framework.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import com.wap.framework.model.AdminUser;

public class LoginDAOImpl implements LoginDAO {

	private JdbcTemplate jdbcTemplate;
	
	private static String admin_login = "select i_adminid,v_username,v_password,v_status from dbo.tb_admin where v_username=? and v_password=? and i_delflag=0";

	public AdminUser login(String username, String password) {
		Object[] params = new Object[] { username, password };
		int[] types = new int[] { Types.VARCHAR, Types.VARCHAR };
		List<?> user = jdbcTemplate.query(admin_login, params, types,
				new AdminUserRowMapper());
		if (user == null || user.size() == 0) {
			return null;
		}
		return (AdminUser) user.get(0);
	}

	class AdminUserRowMapper implements RowMapper {
		public Object mapRow(ResultSet rs, int index) throws SQLException {
			AdminUser au = new AdminUser();
			au.setAdminId(rs.getInt("i_adminID"));
			au.setUsername(rs.getString("v_username"));
			au.setPassword(rs.getString("v_password"));
			au.setStatus(rs.getString("v_status"));
			return au;
		}
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
}
